﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace FlashCards.Tests.NeuralNetworkTest.Simulations
{
    public class RepeatsSimulator
    {
        public static void AddNewRepeat(int [] marks, Model.FlashcardSet set)
        {
            var proxy = new FlashcardServices.SilverlightServiceClient();

            var repeat = new Dto.Repeat()
            {
                FlashcardSetId = set.FlashcardSetId,
                RepeatDate = DateTime.Now,
                UserId = 9,
                Items = new List<Dto.RepeatItem>()
            };

            for (int i = 0; i < set.Flashcards.Count; i++)
            {
                repeat.Items.Add(new Dto.RepeatItem
                    {
                        FlashcardId = set.Flashcards.ElementAt(i).FlashcardId,
                        IsCopied = false,
                        RepeatMark = marks[i]
                    });
            }

            var writer = new StringWriter();
            var serializer = new XmlSerializer(typeof(Dto.Repeat));
            serializer.Serialize(writer, repeat);

            var serializedRepeat = writer.ToString();

            proxy.SaveRepeat(serializedRepeat);
        }

        public static int [] PrepareMarksForSet(Model.FlashcardSet set ,Model.Repeat repeat, double increaseCount, double decreaseCount)
        {
            if (repeat != null)
            {
                var marks = new int[repeat.RepeatItems.Count];
                for (int i = 0; i < marks.Length; i++)
                {
                    var mark = repeat.RepeatItems.ElementAt(i).RepeatMark;
                    marks[i] = mark;
                }

                var rand = new Random();

                while (increaseCount > 0)
                {
                    if (marks.Where(x => x == 3).ToList().Count == marks.Length)
                        break;
                    var index = rand.Next(marks.Length);
                    if (marks[index] < 3)
                    {
                        marks[index]++;
                        increaseCount = increaseCount - 1;
                    }
                }

                while (decreaseCount > 0)
                {
                    if (marks.Where(x => x == 1).ToList().Count == marks.Length)
                        break;
                    var index = rand.Next(marks.Length);
                    if (marks[index] > 1)
                    {
                        marks[index]--;
                        decreaseCount = decreaseCount - 1;
                    }
                }

                return marks;
            }
            else
            {
                var marks = new int[set.Flashcards.Count];
                for (int i = 0; i < marks.Length; i++)
                    marks[i] = 1;
                return marks;
            }
        }
    }
}
